const mutations = {
  addCart(state, info) {
    console.log(info);
    // 1.查看是否添加过
    const oldInfo = state.cartList.find(item => item.iid === info.iid)

    // 2.+1或者新添加
    if (oldInfo) {
      oldInfo.count += 1
    } else {
      info.count = 1
      info.checked = true
      state.cartList.push(info)
    }
  },
  delectCart(state, index) {
    // console.log(info);
    // // 1.查看是否添加过
    // const oldInfo = state.cartList.find(item => item.iid === info.iid)

    // // 2.+1或者新添加
    // if (oldInfo.count>1) {
    //   oldInfo.count -= 1
    // } else {
    //   info.count = 0
    //   info.checked = false
    //   state.cartList.pop(info)
    // }
    state.cartList.splice(index,1)
  },
  decCount(state,index){
    if (state.cartList[index].count>1) {
      state.cartList[index].count--;
    }else{
      state.cartList.splice(index,1)
    }  
  },
  addCount(state,index){
    state.cartList[index].count++; 
  }
}

export default mutations

